package com.rs.game.player.controlers;

import java.util.Set;
import java.util.concurrent.TimeUnit;

import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableSet;
import com.rs.cores.CoresManager;
import com.rs.game.World;
import com.rs.game.WorldObject;
import com.rs.game.WorldTile;
import com.rs.game.map.MapBuilder;
import com.rs.utils.Logger;
import com.rs.utils.Utils;
import com.rs.utils.Utils.EntityDirection;

public class Island extends Controler {

	//Map
	private int[] IslandSpot;

	//Tier
	private int tier;

	//Environment
	private int[] TREES  = new int[] {1276, 66571, 139, 1307, 1307, 1309, 1309, 37823, 37823, 37821};
	private Set<Integer> FISHING_SPOTS = ImmutableSet.of();
	private Set<Integer> CREATURES = ImmutableSet.of();
	private Set<Integer> INVENTORY = ImmutableSet.of();

	/**
	 * Initializing the Island.
	 */
	@Override
	public void start() 
	{
		checkTier();
		createIsland();
		sendInterfaces();
		constructEnvironment();
		teleportPlayer();
		spawnNPCs();

	}

	/**
	 * Teleport player to Island.
	 */
	private void teleportPlayer()
	{
		player.setNextWorldTile(new WorldTile(IslandSpot[0] * 8 + 29, IslandSpot[1] * 8 + 22, 0));
	}

	/**
	 * Constructing Islands Environment.
	 * @param IslandSpot 
	 */
	private void constructEnvironment() 
	{
		WorldTile[] TREE_LOCATIONS = new WorldTile[] { new WorldTile(IslandSpot[0] * 8 + 32, IslandSpot[1] * 8 +17, 0), 
				new WorldTile(IslandSpot[0] * 8 + 33, IslandSpot[1] * 8 +17, 0)	};
		World.spawnObject(new WorldObject(TREES[tier], 10, 2, TREE_LOCATIONS[Utils.random(1)]));

	}

	/**
	 * Prints out the IslandsTier.
	 * @return
	 */
	private String Tier() {
		Joiner joiner = Joiner.on(": ").useForNull(""+getTier()+".");
		return joiner.join("Islands Tier", null); 
	}

	/**
	 * Checking the Islands Tier.
	 */
	private void checkTier() 
	{
		if (getTier() == 1) 
			startTutorial();
		else 
		{

		}
		player.getPackets().sendGameMessage(Tier());


	}

	/**
	 * If tier is one, starts a tutorial.
	 */
	private void startTutorial() 
	{

	}
	/**
	 * Getting the tier of the players island.
	 * @return
	 */
	public int getTier() 
	{
		return tier + 1;
	}

	/**
	 * Creating Island (Maps).
	 */
	private void createIsland()
	{
		try 
		{
			IslandSpot = MapBuilder.findEmptyChunkBound(4, 4);
			MapBuilder.copyAllPlanesMap(354, 320, IslandSpot[0], IslandSpot[1], 8, 8);
			System.out.println("Succesfully created island @ ["+IslandSpot[0] * 8+", "+IslandSpot[1] * 8+"].");
			player.getPackets().sendGameMessage("Succesfully created island @ ["+IslandSpot[0] * 8+", "+IslandSpot[1] * 8+"].");
		} 
		catch (Throwable e) 
		{
			System.out.println(e);		
		}

	}

	/**
	 * Spawns the NPC's.
	 * @param IslandSpot 
	 */
	private void spawnNPCs() 
	{
		World.spawnNPC(3122, new WorldTile(IslandSpot[0] * 8 + 29, IslandSpot[1] * 8 + 22, 0), 0, false, EntityDirection.WEST);//Tutorial NPC.
	}

	/**
	 * Destroy the island. 
	 */
	public void destroyIsland() 
	{
		CoresManager.slowExecutor.schedule(new Runnable() {
			@Override
			public void run() {
				try {
					MapBuilder.destroyMap(IslandSpot[0], IslandSpot[1], 4, 4);
				}catch(Throwable e) {
					Logger.handle(e);
				}
			}
		}, 1200, TimeUnit.MILLISECONDS);
	}
	
	/**
	 * Boolean checks if player tries to magic teleport away.
	 */
    @Override
    public boolean processMagicTeleport(WorldTile toTile) {
	    player.getPackets().sendGameMessage("You can't teleport away from your island; try to use the portal instead.");
	    return false;
    }
    
	/**
	 * Forced stop or close from controler.
	 */
	@Override
	public void forceClose() 
	{
		destroyIsland();
	}
	
}
